package com.rongzhe.house.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.Button;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;
import com.rongzhe.house.R;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.presenter.BasePresenter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.prolificinteractive.materialcalendarview.MaterialCalendarView.SELECTION_MODE_SINGLE;

/**
 * Created by huang on 2018/1/3.
 */

public class DateChooseActivity extends BaseActivity {
    public static final String EXTRA_TIME = "time";
    @BindView(R.id.calendarView)
    MaterialCalendarView calendarView;
    @BindView(R.id.btn_reset)
    Button btnReset;
    @BindView(R.id.btn_ok)
    Button btnOk;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected String getActionTitle() {
        return getString(R.string.choose_date);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_date_choose_filter);
        ButterKnife.bind(this);
        btnReset.setText(android.R.string.cancel);
        btnReset.setEnabled(true);
        calendarView.setSelectionMode(SELECTION_MODE_SINGLE);
        Calendar current = Calendar.getInstance();
        Calendar maxCalendar = Calendar.getInstance();
        maxCalendar.add(Calendar.MONTH, 3);
        calendarView.state().edit().setMinimumDate(current.getTime())
                .setMaximumDate(maxCalendar.getTime()).commit();
        calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget,
                                       @NonNull CalendarDay date, boolean selected) {
                if(selected) {
                    btnOk.setEnabled(true);
                    btnReset.setEnabled(true);
                }
            }
        });
    }

    @OnClick(R.id.btn_ok)
    public void onOkClick() {
        CalendarDay choosed = calendarView.getSelectedDate();
        if (choosed == null) {
            return;
        }
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String time = format.format(choosed.getCalendar().getTime());
        Intent intent = new Intent();
        intent.putExtra(EXTRA_TIME, time);
        setResult(RESULT_OK, intent);
        finish();
    }

    @OnClick(R.id.btn_reset)
    public void onResetClick() {
        setResult(RESULT_CANCELED);
        finish();
    }
}
